import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 测试ReentrantReadWriteLock
 * Created by brss on 2018/3/25.
 */
public class ReadWriteLockTest {
    private ReentrantReadWriteLock rw1 = new ReentrantReadWriteLock();

    public static void main(String[] args) {
        final ReadWriteLockTest test = new ReadWriteLockTest();
        new Thread(){
            @Override
            public void run() {
                test.get(Thread.currentThread());
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                test.get(Thread.currentThread());
            }
        }.start();


    }

    public void get(Thread thread) {
        try {
            rw1.readLock().lock();
//            rw1.writeLock().lock();
            long start = System.currentTimeMillis();
            while (System.currentTimeMillis()-start<=100){
                System.out.println(thread.getName()+"正在进行读操作");
            }
            System.out.println(thread.getName()+"读操作完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rw1.readLock().unlock();
//            rw1.writeLock().unlock();
        }

    }
    /*public synchronized void get(Thread thread) {
        long start = System.currentTimeMillis();
        while (System.currentTimeMillis()-start<=1){
            System.out.println(thread.getName()+"正在进行读操作");
        }
        System.out.println(thread.getName()+"读操作完毕");

    }*/
}
